<?php
namespace app\index\controller;

use app\admin\model\Classes;
use app\admin\model\College;
use app\common\controller\Frontend;
use think\Session;
use think\helper\Time;
class Formstudent extends Form
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'weiui';
    protected $model = null;
    protected $stumodel = null;

    public function _initialize()
    {

        parent::_initialize();
        $this->model = new \app\admin\model\FormStudent;
        $this->stumodel = new \app\admin\model\Student;

    }
    public function index()
    {
        $title = '学生防疫信息统计';
        $this -> assign('pageTitle',$title);
        $this -> assign('title',$title);
        $this -> assign('wxcode','student');

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $where= [
                'classes_id' => $params['classes_id'],
                'nickname' => $params['nickname'],
                'sid' => $params['sid']
            ];
            $row = $this->stumodel->where($where)->find();
            if(!$row)
                $this->error('学号、姓名填写有误,请检查！',null,0);

            $openid = Session::get('wechatUser')['openid'];

            $params['college_id'] = $row->classes->college_id;
            $params['college_name'] = $row->classes->college->name;

            //var_dump($params);

            if(isset($params['abc']) && $params['abc'] && isset($params['longitude']) && $params['longitude']){
                $address = getAddress($params['longitude'],$params['abc']);

                if($address){
                    $params['formatted_address'] = $address['formatted_address'];
                    $params['city'] = $address['city'];
                    $params['province'] = $address['province'];
                    $params['district'] = $address['district'];
                    $params['street'] = $address['street'];
                }
            }


            $today = Time::today();
            $today_row = $this->model->where(['openid'=>$openid])->whereTime('create_time','between',$today)->find();

            if($today_row)
            {
                $res = $today_row->allowField(true)->save($params);
                $this->success('上报成功','/student',0);
            }
            else
            {
                $params['openid'] = $openid;
                $res = $this->model->allowField(true)->save($params);
                $row->is_reported = 1;
                $row->save();
                if ($res)
                    $this->success('上报成功','/student',0);
                else
                    $this->error('上报失败','/student',0);
            }

            // $house=$this->model->where(['user_id'=>$this->id,''])->find()
            // print_r($data);
        }
        // $user = Session::get('frontUser');
        // $row = $this->model ->field(['id','openid','classid','create_time'],true)-> where('openid',$user['openid'])->order('id','desc')->find();
        // $formdata = [];
        // foreach ($row as $key => $value) {
        //     # code...
        // }

        $wxjs = $this -> app->jssdk->buildConfig(array('getLocation'), false);
        $this -> assign('wxjs',$wxjs);
        return $this->view->fetch();
    }


    public function getclasses(){
        $classes = Classes::all();
        $data = [];

        foreach ($classes as $k => $v){
            // if(!isset($data[$v['college_id']])){
            //     $data[$v['college_id']] = [
            //         'label' => $collegesArr[$v['college_id']],
            //         'value' => $v['college_id'],
            //         'children'  => []
            //     ];
            // }

            if(!isset($data[$v['year_']])){
                $data[$v['year_']] = [
                    'label' => $v['year_'].'级',
                    'value' => $v['year_'],
                    'children'  => []
                ];
            }

            if(!isset($data[$v['year_']]['children'][$v['id']])){
                $data[$v['year_']]['children'][$v['id']] = [
                    'label' => $v['name'],
                    'value' => $v['id']
                ];
            }

        }

        foreach ($data as $k => &$v){
            // foreach ($v['children'] as $kv => &$vv){
            //     $vv['children'] = array_values($vv['children']);
            // }
            $v['children'] = array_values($v['children']);
        }


        echo  json_encode(array_values($data));

    }

}
